home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10734 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  158 lines

  1. Path: news.db.erau.edu!kochank
  2. From: kochank@news.db.erau.edu (Konrad Kochan)
  3. Newsgroups: comp.lang.c++
  4. Subject: PLEASE Help! :(
  5. Date: 9 Mar 1996 20:48:46 GMT
  6. Organization: Embry-Riddle Aeronautical University
  7. Message-ID: <4hsqre$4d7@deadbird.db.erau.edu>
  8. NNTP-Posting-Host: 155.31.1.1
  9. NNTP-Posting-User: kochank
  10. Keyword: 
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Howdy.. I have a program to write, but I'm clueless... Now I could
  14. give you a 100 excuses and blame the instructor (and I should cause
  15. all of it would be true.. :)
  16.  
  17. Anyways, I'm clueless.. can someone, a good samaritan (or someone just
  18. bored..) help me out!
  19.  
  20. Here is the program statement..
  21.  
  22. Write a C program to sort, using any sorting method, a text file
  23. named RECORDS.DAT, of unknown length, of records organized as follows:
  24.  
  25. Field 1.  Contains an integer.
  26. Field 2.  Contains a string of exactly 20 characters.
  27. Field 3.  Contains a float followed by a newline character.
  28.  
  29. Example of file organization:
  30.  
  31. 234 Numa Chaikin        33000.0
  32.  
  33.  
  34. 234 Numa Chaikin        33000.0
  35.  75 advertisements      55.55
  36. 901 Shepard             15255.5
  37.   3 Brook               120050.0
  38. 888 non-descriptive     0.0
  39.  
  40. Sorting has to be done lexicographically, that is, by the second field
  41. using alphabetical order.  This means that:
  42.  
  43. 1. Upper-case and lower-case letters should be treated identically.
  44.    For example, the right order for a part of the list above is:
  45.    888 non-descriptive 0.0
  46.    234 Numa Chaikin 33000.0
  47.  
  48. 2. The only punctuation sign used in strings is a dash '-', which
  49.    should be ignored in sorting (but not removed from words).
  50.  
  51.  
  52. ~
  53. ~
  54. ~
  55. ~
  56.  
  57. And here is what I got so far.. which DOESN'T work (right or otherwise, I
  58. can't even get the program to scan the right fields from the file.. :(
  59.  
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <string.h>
  63.  
  64. /* Global Variable Definition */
  65.  
  66. int IntArr[512][5];
  67. char WordsArr1[512][10];
  68. char WordsArr2[512][10];
  69. float FloatArr[512][5];
  70. char inbuff[80];
  71. int index=0;
  72.  
  73. /* Function declaration */
  74.  
  75. void Welcome(void);
  76. void Goodbye(void);
  77.  
  78. main()
  79. {
  80.  
  81. main()
  82. {
  83. /* Next function opens a file, if the file is missing, it prints
  84.  an error message */
  85.  
  86. int i;
  87.  FILE * fin, * fout;
  88.     if (!(fin = fopen("RECORDS.DAT", "r")))
  89.          {
  90.         printf("Something is wrong: Probably file missing.\n");
  91.         exit(1);   /* Program exits here if file is missing. */
  92.           }
  93.         printf("\nInput File = RECORDS.DAT\n");
  94. /* This function prints a Welcome message */
  95.  
  96. /* Welcome(); */
  97.  
  98. /* This function scans in the array */
  99.  
  100.         while (fgets(inbuff,80,fin) !=NULL)
  101. {
  102. sscanf(inbuff,"%d %s %s %4.2f", &IntArr[index],&WordsArr1[index],&WordsArr2[inde
  103. x],&FloatArr[index]);
  104.  
  105. {
  106. sscanf(inbuff,"%d %s %s %4.2f", &IntArr[index],&WordsArr1[index],&WordsArr2[inde
  107. x],&FloatArr[index]);
  108. index++;
  109. }
  110.  
  111. /* Opens the output file */
  112.  
  113. if (!(fout = fopen("test", "w")))
  114.          {
  115.         printf("Something is wrong: File might be write protected.\n");
  116.         exit(1);   /* Program exits here if file can't be opened. */
  117.           }
  118.  
  119.     for (i=0; i<index; i++)
  120.         fprintf(fout, "%d %s %s %4.2f\n",  IntArr[i], WordsArr1[i],WordsArr2[i],
  121.  
  122.         FloatArr[i]);
  123.  
  124. /* Prints the sorted array to the screen */
  125.  
  126.     for (i=0; i<index; i++)
  127.         printf("Original array: %d %s %s %4.2f \n", IntArr[i],WordsArr1[i],
  128.  
  129.     for (i=0; i<index; i++)
  130.         printf("Original array: %d %s %s %4.2f \n", IntArr[i],WordsArr1[i],
  131. WordsArr2[i],FloatArr[i]);
  132. fclose(fin);
  133. fclose(fout);
  134. }
  135.  
  136.  
  137. ~
  138. ~
  139. ~
  140. ~
  141. ~
  142. ~
  143. ~
  144. ~
  145. ~
  146. ~
  147. ~
  148. ~
  149. ~
  150. ~
  151. ~
  152.  
  153. If you can either give me hints, rewrite the above, or write a totaly
  154. new program I would appreciate it.. I know its a lot to ask for (to do some-
  155. one elses homework pretty much..) but I'm really clueless..
  156.  
  157. Thank you in advance!
  158.